package com.eascs.workflow.auditorReplace.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.workflow.auditorReplace.dao.IAuditorReplaceDao;
import com.eascs.workflow.auditorReplace.vo.AuditorReVo;
import com.eascs.workflow.common.dao.BaseDao;

import jodd.util.StringUtil;

@Repository
public class AuditorReplaceDaoImpl extends BaseDao implements IAuditorReplaceDao {

	@Override
	public PageResult<Object[]> getAuditorListInProcess(AuditorReVo vo) {

		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());

		StringBuffer querySqlBuf = new StringBuffer("SELECT  a.node_auditor_id nodeAuditorId, a.auditor_no userNo,a.auditor_name userName ");
		querySqlBuf.append(" ,m.process_name processName, m.org_name orgName , r.role_name roleName , m.org_id orgId , r.id roleNo");
		querySqlBuf.append(" FROM wf_process_task_main m LEFT JOIN wf_process_task_node n ON m.task_id=n.task_id LEFT JOIN wf_task_node_auditors a"
				+ " ON n.task_node_id=a.task_node_id LEFT JOIN o2o_act_role r ON  n.role_no = r.id");
		querySqlBuf.append(" WHERE m.process_status = '2' AND n.node_status in ('1','2') AND a.auditor_status in ('0','1') ");
		
		if (StringUtils.isNotBlank(vo.getUserNo())) {
			querySqlBuf.append(" AND a.auditor_no = ?");
			sqlPageQuery.addParam(vo.getUserNo());
		}
		if (StringUtils.isNotBlank(vo.getNodeAuditorId())) {
			querySqlBuf.append(" AND a.node_auditor_id = ?");
			sqlPageQuery.addParam(vo.getNodeAuditorId());
		}
		sqlPageQuery.setQuerySql(querySqlBuf.toString());
		return super.findDataWithPage(sqlPageQuery);
	}

	@Override
	public List<Object[]> getLikeEmployeeList(AuditorReVo vo) {
		String sql = "SELECT employee_no, employee_namecn, CONCAT(employee_code,employee_account) FROM o2o_employee WHERE employee_namecn LIKE ? LIMIT 10";
		return super.findObjectsList(sql, "%" + vo.getUserName() + "%");
	}

	@Override
	public int updateAuditor(AuditorReVo vo) {
		StringBuffer sqlBuffer = new StringBuffer("UPDATE wf_task_node_auditors w SET w.auditor_no = ? ");
		sqlBuffer.append(" , w.auditor_name = ? , w.auditor_des = ? WHERE w.node_auditor_id = ?");
		Object[] params = {vo.getUserNo(),vo.getUserName(),vo.getAuditorDes(),vo.getNodeAuditorId()};
		return super.execute(sqlBuffer.toString(), params);
	}

	@Override
	public PageResult<Object[]> getReAuditor(AuditorReVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, vo.getPageIndex());
		StringBuffer querySqlBuf = new StringBuffer("SELECT a.unit_no orgId , u.org_name orgName , u.role_name roleName, u.user_no userNo ");
		querySqlBuf.append("  FROM act_org_role_user u , act_org a WHERE u.is_remove = '0' AND a.id = u.org_id ");
		if (!StringUtil.isBlank(vo.getRoleNo())) {
			querySqlBuf.append(" AND u.role_id = ? ");
			sqlPageQuery.addParam(vo.getRoleNo());
		} 
		if (!StringUtil.isBlank(vo.getUserNo())) {
			querySqlBuf.append(" AND u.user_no = ? ");
			sqlPageQuery.addParam(vo.getUserNo());
		} 
		sqlPageQuery.setQuerySql(querySqlBuf.toString());
		return super.findDataWithPage(sqlPageQuery);
	}
	
	
	
	
	
}
